<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>
        项目信息
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" th:href="@{/css/main.css}" media="all">
    <script type="text/javascript" th:src="@{/lib/loading/okLoading.js}"></script>
    <!-- 引入ElementUI样式 -->
    <link rel="stylesheet" th:href="@{/css/index.css}">
</head>
<body>
<div id="app">
    <div class="x-nav">
            <span class="layui-breadcrumb">
              <a><cite>首页</cite></a>
              <a><cite>疫情管理</cite></a>
              <a><cite>{{title}}</cite></a>
            </span>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
           href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="x-body">
        <el-form :model="surveyDate" :rules="rules" ref="surveyDateFormRef" label-width="120px" class="demo-ruleForm">
            <el-form-item label="问题名称" prop="question">
                <el-input v-model="surveyDate.question"></el-input>
            </el-form-item>
            <el-form-item label="创建时间" prop="createTime">
                <el-col :span="11">
                    <el-date-picker type="date" placeholder="选择日期" v-model="surveyDate.createTime"
                                    style="width: 100%;"></el-date-picker>
                </el-col>
                <el-col class="line" :span="2">——————</el-col>
                <el-col :span="11">
                    <el-time-picker placeholder="选择时间" v-model="surveyDate.createTime"
                                    style="width: 100%;"></el-time-picker>
                </el-col>
            </el-form-item>
            <el-form-item label="问题类型" prop="answerType">
                <el-select v-model="surveyDate.answerType" placeholder="请选择">
                    <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value" @click.native="boolS(item.value)">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item
                    v-if="bool"
                    v-for="(domain, index) in domains"
                    :label="'选项' + (index+1)"
                    :key="domain.key">
                <el-input v-model="domain.choiceText"></el-input>
                <el-button @click.prevent="removeDomain(domain)">删除</el-button>
            </el-form-item>
            <el-form-item v-if="bool">
                <el-button @click="addDomain">新增</el-button>
            </el-form-item>
            <el-form-item style="display:none">
                <el-input v-model="surveyDate.surveyId"></el-input>
            </el-form-item>
            <el-form-item style="display:none">
                <el-input v-model="surveyDate.questionId"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="save()" style="text-align: center">保存</el-button>
                <el-button @click="clearAll()" style="text-align: center">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</div>
<script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/js/x-layui.js}" charset="utf-8"></script>
<!--导入Vue依赖包-->
<script th:src="@{/js/vue.js}" charset="utf-8"></script>
<!-- 引入Element组件库 -->
<script th:src="@{/js/index.js}"></script>
<!--导入Axios依赖包-->
<script th:src="@{/js/axios.js}" charset="utf-8"></script>
<script type="text/javascript">
    var app = new Vue({
        el: "#app",
        data: {
            bool: false,
            options: [{
                value: '单选',
                label: '单选'
            }, {
                value: '多选题',
                label: '多选题'
            }, {
                value: '简答题',
                label: '简答题'
            }],
            title: "添加问卷题目",
            surveyDate: {
                surveyId: '',
                questionId:null,
                domain:[]
            },
            domains: [],
            fileList: [],
            rules: {
                question: [
                    {required: true, message: '请输入问题名称', trigger: 'blur'}
                ],
                answerType: [
                    {required: true, message: '请选择问题类型', trigger: 'blur'}
                ],
                createTime: [
                    {required: true, message: '请选择创建时间', trigger: 'blur'}
                ]
            }
        },
        methods: {
            surveyAnswersBy(questionId){
                axios.get("/surveyDate/findByquestionId?questionId=" + questionId).then((res) => {
                    for (var i=0;i<res.data.data.length;i++){
                        var ads={choiceText:''};
                        ads.choiceText=res.data.data[i].choiceText;
                        this.domains.push(ads);
                    }
                });
                console.log(this.domains)
                this.bool=true;
            },
            removeDomain(item) {
                var index = this.domains.indexOf(item)
                if (index !== -1) {
                    this.domains.splice(index, 1)
                }
            },
            addDomain() {
                this.domains.push({
                    choiceText: '',
                    key: Date.now()
                });
            },
            boolS(xuangx) {
                if (xuangx != "简答题") {
                    this.bool = true;
                } else {
                    this.bool = false;
                }
                this.domains=[{choiceText: ''}];
            },
            findById() {
                axios.get("/surveyDate/findById?id=" + this.surveyDate.questionId).then((res) => {
                    this.surveyDate = res.data.data;
                });
            },
            //表达重置
            clearAll() {
                this.$refs.surveyDateFormRef.resetFields();
                if (this.fileList.length > 0) {
                    this.handleRemove();
                }
            },
            save() {
                //选项提交
                var ss=[];
                for(var i=0;i<this.domains.length;i++){
                    var tm=this.domains[i].choiceText;
                    ss[i]=tm;
                }
                this.surveyDate.domain=ss;
                var method = "";
                if (this.surveyDate.questionId != null) {
                    method = "update"
                } else {
                    method = "add"
                }
                axios.post("/surveyDate/" + method, this.surveyDate).then((res) => {
                    if (res.data.flag) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }
                });
            },
            getQueryString(name) {
                let reg = `(^|&)${name}=([^&]*)(&|$)`
                let r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]);
                return null;
            }
        },
        created: function () {
            layui.use(['element', 'layer', 'form'], function () {
                $ = layui.jquery;//jquery
                lement = layui.element();//面包导航
                layer = layui.layer;//弹出层
                form = layui.form();
                okLoading.close($);
            });
            //获取父页面传递参数
            this.surveyDate.questionId = this.getQueryString("id");
            this.surveyDate.surveyId = this.getQueryString("surveyId");
            //根据小区ID获取小区对象，进行数据回显
            if (this.surveyDate.questionId != null && this.surveyDate.questionId != '') {
                this.title = "修改问卷题目";
                this.findById();
                this.surveyAnswersBy(this.surveyDate.questionId);
            }
        }
    });
</script>
</body>
</html>
